<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="出库单号">
              <a-input placeholder="请输入出库单号" v-model="queryParam.outboundNo"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="客户名称">
              <a-input placeholder="请输入客户名称" v-model="queryParam.customerName"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="状态">
              <j-dict-select-tag placeholder="请选择状态" v-model="queryParam.status" dictCode="outbound_status" />
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <a-button type="primary" icon="download" @click="handleExportXls('成品出库单')">导出</a-button>
      <a-button type="primary" icon="upload" @click="handleImportExcel">导入</a-button>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete" />删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /> </a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择
        <a style="font-weight: 600">{{ selectedRowKeys.length }}</a> 项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{ x: 1400 }"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        class="j-table-force-nowrap"
        @change="handleTableChange"
      >
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">无图片</span>
          <img
            v-else
            :src="getImgView(text)"
            height="25px"
            alt=""
            style="max-width: 80px; font-size: 12px; font-style: italic"
          />
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">无文件</span>
          <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleViewDetail(record)">查看明细</a>
          <a-divider type="vertical" />
          <a @click="handleEdit(record)">编辑</a>
          <a-divider type="vertical" />

          <a-dropdown>
            <a class="ant-dropdown-link"> 更多 <a-icon type="down" /> </a>
            <a-menu slot="overlay">
              <a-menu-item v-if="record.status === 0">
                <a @click="handleAudit(record)">审核</a>
              </a-menu-item>
              <a-menu-item v-if="record.status === 1">
                <a @click="handleExecute(record)">执行出库</a>
              </a-menu-item>
              <a-menu-item v-if="record.status !== 3 && record.status !== 4">
                <a @click="handleCancel(record)">取消</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
      </a-table>
    </div>

    <!-- 表单区域 -->
    <finished-goods-outbound-modal ref="modalForm" @ok="modalFormOk"></finished-goods-outbound-modal>

    <!-- 出库明细查看模态框 -->
    <finished-goods-outbound-detail-modal ref="outboundDetailModal"></finished-goods-outbound-detail-modal>

    <!-- 审核弹窗 -->
    <outbound-audit-modal ref="auditModal" @ok="modalFormOk"></outbound-audit-modal>

    <!-- 执行出库弹窗 -->
    <outbound-execute-modal ref="executeModal" @ok="modalFormOk"></outbound-execute-modal>
  </a-card>
</template>

<script>
import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import FinishedGoodsOutboundModal from './modules/FinishedGoodsOutboundModal'
import FinishedGoodsOutboundDetailModal from './modules/FinishedGoodsOutboundDetailModal'
import OutboundAuditModal from './modules/OutboundAuditModal'
import OutboundExecuteModal from './modules/OutboundExecuteModal'
import { postAction } from '@/api/manage'
import { filterObj } from '@/utils/util'

export default {
  name: 'FinishedGoodsOutboundList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    FinishedGoodsOutboundModal,
    FinishedGoodsOutboundDetailModal,
    OutboundAuditModal,
    OutboundExecuteModal,
  },
  data() {
    return {
      description: '成品出库单管理页面',
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          },
        },
        {
          title: '出库单号',
          align: 'center',
          dataIndex: 'outboundNo',
          width: 220,
        },
        {
          title: '订单编号',
          align: 'center',
          dataIndex: 'orderNo',
          width: 150,
        },
        {
          title: '客户名称',
          align: 'center',
          dataIndex: 'customerName',
          width: 150,
        },
        {
          title: '项目名称',
          align: 'center',
          dataIndex: 'projectName',
          width: 150,
        },
        {
          title: '出库总数',
          align: 'center',
          dataIndex: 'totalQuantity',
          width: 100,
        },
        {
          title: '状态',
          align: 'center',
          dataIndex: 'status',
          width: 100,
          customRender: (text) => {
            const statusMap = {
              0: '待审核',
              1: '审核通过',
              2: '审核驳回',
              3: '已出库',
              4: '已取消',
            }
            return statusMap[text] || text
          },
        },
        {
          title: '审核时间',
          align: 'center',
          dataIndex: 'auditTime',
          width: 150,
        },
        {
          title: '审核人姓名',
          align: 'center',
          dataIndex: 'auditPersonName',
          width: 120,
        },
        {
          title: '审核意见',
          align: 'center',
          dataIndex: 'auditRemark',
          width: 150,
          ellipsis: true,
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 200,
          scopedSlots: { customRender: 'action' },
        },
      ],
      url: {
        list: '/wms/finished-goods/outbound/list',
        delete: '/wms/finished-goods/outbound/delete',
        deleteBatch: '/wms/finished-goods/outbound/deleteBatch',
        exportXlsUrl: '/wms/finished-goods/outbound/exportXls',
        importExcelUrl: '/wms/finished-goods/outbound/importExcel',
      },
      dictOptions: {},
      superFieldList: [],
    }
  },
  created() {
    this.getSuperFieldList()
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
  },
  methods: {
    initDictConfig() {
      // 初始化字典配置
    },
    getSuperFieldList() {
      let fieldList = []
      fieldList.push({ type: 'string', value: 'outboundNo', text: '出库单号', dictCode: '' })
      fieldList.push({ type: 'string', value: 'orderNo', text: '订单编号', dictCode: '' })
      fieldList.push({ type: 'string', value: 'customerName', text: '客户名称', dictCode: '' })
      fieldList.push({ type: 'string', value: 'projectName', text: '项目名称', dictCode: '' })
      fieldList.push({ type: 'int', value: 'totalQuantity', text: '出库总数', dictCode: '' })
      fieldList.push({ type: 'int', value: 'status', text: '状态', dictCode: 'outbound_status' })
      fieldList.push({ type: 'Date', value: 'auditTime', text: '审核时间', dictCode: '' })
      fieldList.push({ type: 'string', value: 'auditPersonName', text: '审核人姓名', dictCode: '' })
      fieldList.push({ type: 'string', value: 'auditRemark', text: '审核意见', dictCode: '' })
      this.superFieldList = fieldList
    },
    handleAdd: function () {
      this.$refs.modalForm.add()
      this.$refs.modalForm.title = '新增'
    },
    handleEdit: function (record) {
      if (record.status !== 0) {
        this.$message.warning('只有待审核状态的出库单才能编辑！')
        return
      }
      this.$refs.modalForm.edit(record)
      this.$refs.modalForm.title = '编辑'
    },
    handleViewDetail: function (record) {
      // 查看出库明细
      this.$refs.outboundDetailModal.show(record)
    },
    handleAudit: function (record) {
      this.$refs.auditModal.show(record)
    },
    handleExecute: function (record) {
      this.$refs.executeModal.show(record)
    },
    handleCancel: function (record) {
      let that = this
      this.$confirm({
        title: '确认取消',
        content: '确定要取消这个出库单吗？',
        okText: '确定',
        okType: 'danger',
        cancelText: '取消',
        onOk() {
          return new Promise((resolve, reject) => {
            postAction('/wms/finished-goods/outbound/cancel', { id: record.id, reason: '用户手动取消' })
              .then((res) => {
                if (res.success) {
                  that.$message.success('取消成功！')
                  that.loadData()
                  resolve()
                } else {
                  that.$message.error(res.message || '取消失败！')
                  reject(new Error(res.message || '取消失败！'))
                }
              })
              .catch((error) => {
                reject(error)
              })
          })
        },
      })
    },
    onApplyTimeChange: function (dates, dateStrings) {
      if (dates && dates.length === 2) {
        this.queryParam.applyStartTime = dateStrings[0] + ' 00:00:00'
        this.queryParam.applyEndTime = dateStrings[1] + ' 23:59:59'
      } else {
        this.queryParam.applyStartTime = ''
        this.queryParam.applyEndTime = ''
      }
    },
    getQueryParams() {
      // 获取查询条件
      let sqp = {}
      if (this.superQueryParams) {
        sqp['superQueryParams'] = encodeURI(this.superQueryParams)
        sqp['superQueryMatchType'] = this.superQueryMatchType
      }
      var param = Object.assign(sqp, this.queryParam, this.isorter, this.filters)
      param.field = this.getQueryField()
      param.pageNo = this.ipagination.current
      param.pageSize = this.ipagination.pageSize
      return filterObj(param)
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>
